/**
 * Copyright (c) 2020-2021 浦东大爷 All rights reserved.
 * 机会永远都是留给有准备的人
 */
package com.lagou.controller;

import com.lagou.domain.Customer;
import com.lagou.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author: gitwo
 * @date: 2021-04-23
 * @description:
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /*
        查询所有用户
     */

    @RequestMapping("/findAll")
    public  String findAll(Model model){

        //实现查询所有账户
        List<Customer> list = customerService.findAll();

        // 把封装好的list存到model中
        model.addAttribute("list",list);

        return "list";


    }

    @RequestMapping("/save")
    public String save(Customer customer){

        customerService.save(customer);
        // 跳转到findAll方法从新查询一次数据库进行数据的遍历展示
        return "redirect:/customer/findAll";
    }


    /*
        根据id查询账户信息，完成账户回显
     */
    @RequestMapping("/findById")
    public String findById(Integer id,Model model){

        Customer customer =  customerService.findById(id);

        //存到model中
        model.addAttribute("customer",customer);

        //视图跳转
        return  "update";

    }


    /*
        更新账户
     */
    @RequestMapping("/update")
    public String update(Customer customer){
        customerService.update(customer);
        return "redirect:/customer/findAll";
    }


    /*
        批量删除
     */
    @RequestMapping("/deleteBatch")
    public String deleteBatch(Integer[] ids){

        customerService.deleteBatch(ids);

        return "redirect:/customer/findAll";

    }

    /**
     * 根据ID删除
     */
    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        customerService.deleteById(id);
        return "redirect:/customer/findAll";
    }
}
